﻿// Decompiled with JetBrains decompiler
// Type: MCM.Implementation.Global.GlobalSettingsContainer
// Assembly: MCMv5, Version=5.10.2.0, Culture=neutral, PublicKeyToken=null
// MVID: 7AB326B9-B2B4-41C2-9177-68E944071101
// Assembly location: D:\steam\steamapps\workshop\content\261550\2859238197\bin\Win64_Shipping_Client\MCMv5.dll

using BUTR.DependencyInjection.Logger;
using HarmonyLib.BUTR.Extensions;
using MCM.Abstractions;
using MCM.Abstractions.Base.Global;
using MCM.Abstractions.Global;
using MCM.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

#nullable enable
namespace MCM.Implementation.Global
{
  internal sealed class GlobalSettingsContainer : 
    BaseGlobalSettingsContainer,
    IGlobalSettingsContainer,
    ISettingsContainer,
    ISettingsContainerHasSettingsDefinitions,
    ISettingsContainerCanOverride,
    ISettingsContainerCanReset,
    ISettingsContainerPresets,
    ISettingsContainerHasSettingsPack
  {
    public GlobalSettingsContainer(IBUTRLogger<GlobalSettingsContainer> logger)
    {
      foreach (GlobalSettings globalSetting in GetGlobalSettings())
      {
        logger.LogTrace(string.Format("Registering settings {0}.", (object) globalSetting.GetType()));
        this.RegisterSettings(globalSetting);
      }

      IEnumerable<GlobalSettings> GetGlobalSettings()
      {
        foreach (Assembly assembly in AccessTools2.AllAssemblies().Where<Assembly>((Func<Assembly, bool>) (a => !a.IsDynamic)))
        {
          IEnumerable<GlobalSettings> settings;
          try
          {
            settings = (IEnumerable<GlobalSettings>) ((IEnumerable<Type>) AccessTools2.GetTypesFromAssemblyIfValid(assembly)).Where<Type>((Func<Type, bool>) (t => t.IsClass && !t.IsAbstract)).Where<Type>((Func<Type, bool>) (t => (object) t.GetConstructor(Type.EmptyTypes) != null)).Where<Type>((Func<Type, bool>) (t => typeof (GlobalSettings).IsAssignableFrom(t))).Where<Type>((Func<Type, bool>) (t => !typeof (EmptyGlobalSettings).IsAssignableFrom(t))).Where<Type>((Func<Type, bool>) (t => !typeof (IWrapper).IsAssignableFrom(t))).Select<Type, GlobalSettings>((Func<Type, GlobalSettings>) (t =>
            {
              try
              {
                return Activator.CreateInstance(t) as GlobalSettings;
              }
              catch (Exception ex1)
              {
                try
                {
                  logger.LogError(ex1, string.Format("Failed to initialize type {0}", (object) t));
                }
                catch (Exception ex2)
                {
                  logger.LogError(ex1, "Failed to initialize and log type!");
                }
                return (GlobalSettings) null;
              }
            })).OfType<GlobalSettings>().ToList<GlobalSettings>();
          }
          catch (TypeLoadException ex)
          {
            settings = (IEnumerable<GlobalSettings>) Array.Empty<GlobalSettings>();
            logger.LogError((Exception) ex, string.Format("Error while handling assembly {0}!", (object) assembly));
          }
          foreach (GlobalSettings setting in settings)
            yield return setting;
          settings = (IEnumerable<GlobalSettings>) null;
        }
      }
    }
  }
}
